package com.wsd.jing.controller.system;

import com.wsd.jing.annotation.ControllerLog;
import com.wsd.jing.entity.Department;
import com.wsd.jing.request.CommonRequest;
import com.wsd.jing.request.CommonResponse;
import com.wsd.jing.service.IDepartmentService;
import com.wsd.jing.validator.group.InsertGroup;
import com.wsd.jing.validator.group.UpdateGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * All rights Reserved, Designed By www.tom.com
 *
 * @author tom
 * @version 1.0.0
 * @projectName: jing
 * @packageName: com.wsd.jing.controller
 * @className: DepartmentController
 * @description: 部门前端控制器
 * @createDate 2022-11-29 04:06
 * @copyright 2022-11-29 04:06 www.tom.com Inc. All rights reserved.
 */
@Api(tags = "部门")
@Validated
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/rest/department")
public class DepartmentController {

    private final IDepartmentService departmentService;

    @ApiOperation(value = "新增")
    @PostMapping()
    @ControllerLog(action = "新增部门", message = "新增部门失败")
    // @SaCheckPermission("department:add")
    public CommonResponse insertDepartment(@Validated(InsertGroup.class) @RequestBody Department department) {
        return CommonResponse.success(departmentService.insertDepartment(department), "新增成功");
    }

    @ApiOperation(value = "修改")
    @PutMapping()
    @ControllerLog(action = "修改部门", message = "修改部门失败")
    // @SaCheckPermission("department:update")
    public CommonResponse updateDepartment(@Validated(UpdateGroup.class) @RequestBody Department department) {
        return CommonResponse.success(departmentService.updateDepartment(department), "修改成功");
    }

    @ApiOperation(value = "删除")
    @ApiImplicitParam(name = "id", value = "部门id", paramType = "path", dataType = "Integer", required = true)
    @DeleteMapping("/{id}")
    @ControllerLog(action = "删除部门", message = "删除部门失败")
    // @SaCheckPermission("department:delete")
    public CommonResponse deleteDepartment(@PathVariable("id") Integer id) {
        return CommonResponse.success(departmentService.deleteDepartment(id), "删除成功");
    }

    @ApiOperation(value = "批量删除")
    @ApiImplicitParam(name = "ids", value = "部门id逗号分割字符", paramType = "query", dataType = "String", required = true)
    @DeleteMapping("")
    @ControllerLog(action = "批量删除部门", message = "批量删除部门失败")
    // @SaCheckPermission("department:batch:delete")
    public CommonResponse deleteDepartments(@RequestParam("ids") String ids) {
        return CommonResponse.success(departmentService.deleteDepartments(ids), "批量删除成功");
    }

    @ApiOperation(value = "查询")
    @ApiImplicitParam(name = "id", value = "部门id", paramType = "path", dataType = "Integer", required = true)
    @GetMapping("/{id}")
    @ControllerLog(action = "查询部门", message = "查询部门失败")
    // @SaCheckPermission("department:get")
    public CommonResponse getDepartment(@PathVariable("id") Integer id) {
        return CommonResponse.success(departmentService.getDepartment(id), "查询成功");
    }

    @ApiOperation(value = "查询列表")
    @GetMapping("/list")
    @ControllerLog(action = "查询部门列表", message = "查询部门列表失败")
    // @SaCheckPermission("department:list")
    public CommonResponse listDepartments(CommonRequest commonRequest, Department department) {
        return CommonResponse.success(departmentService.listDepartments(commonRequest, department), "查询成功");
    }

    @ApiOperation(value = "分页查询")
    @GetMapping("/pages")
    @ControllerLog(action = "分页查询部门", message = "分页查询部门失败")
    // @SaCheckPermission("department:page")
    public CommonResponse listPageDepartments(CommonRequest commonRequest, Department department) {
        return CommonResponse.success(departmentService.listPageDepartments(commonRequest, department), "查询成功");
    }

    @ApiOperation(value = "查询部门树")
    @GetMapping("/tree")
    @ControllerLog(action = "查询部门树", message = "查询部门树失败")
    // @SaCheckPermission("department:tree")
    public CommonResponse listTreeDepartments() {
        return CommonResponse.success(departmentService.listTreeDepartments(), "查询成功");
    }
}
